---------------------------------------------------------------------------------------------------
--endtitles
---------------------------------------------------------------------------------------------------
films_endtitles = pause {
   bg           = "bg"
  ,time         = 1
  ,next         = chain_ {
                    mute_(0,0),
                    music_("bam2"),
                    walk_("films_endtitles0")
                  }
}

films_endtitles0 = pause {
   time         = 200
  ,next         = walk_("films_endtitles1")
}

films_endtitles1 = gfxroom {
   letter       = function(s,spr,fnt,c,x)
                    local t = sprite.text(fnt, c, "white");
                    sprite.draw(t, spr, x, 0);
                    sprite.free(t);
                  end
  ,word         = function(s)
                    local fnt = drawing.font("sans-b.ttf", 92);
                    local spr = sprite.box(800, 300, "black");
                    local xs = 70;
                    s:letter(spr,fnt,"Л",xs);
                    s:letter(spr,fnt,"И",xs+150);
                    s:letter(spr,fnt,"Д",xs+300);
                    s:letter(spr,fnt,"И",xs+450);
                    s:letter(spr,fnt,"Я",xs+600);
                    return spr;
                  end
  ,enter        = function(s)
                    s.spr = s:word();
                    sprite.copy(s.spr, s.pic, 0, 226);
                  end
  ,ontimer      = function(s)
                    if s._frames == 150 then
                      play_music("spring",2);
                    end
                    if s._frames < 160 then
                      return;
                    end
                    local y = 226;
                    if s.spr == nil then
                      s.spr = s:word();
                    end
                    if s._x == nil then
                      s._x = 0;
                    end
                    sprite.copy(s.spr, s.pic, s._x, y);
                    s._x = s._x - 2;
                  end
  ,dispose      = function(s)
                    if s.spr ~= nil then
                      sprite.free(s.spr);
                      s.spr = nil;
                    end
                    s._x = nil;
                  end
  ,stop         = 540--360
  ,next         = walkfast2_("films_endtitles2")
  ,rate         = 50
}

films_endtitles2 = gfxroom {
   ontimer      = function(s)
                    if s._num == nil then
                      s._num = 0;
                    end
                    local yy = 200;
                    local pos = 0;
                    if s.spritelen == 0 then
                      pos = 699;
                    end
                    
                    for i,v in pairs(s.sprites) do
                      if v ~= nil then
                        v.x = v.x - 2;
                        local dy = (600 - v.h) / 2;
                        sprite.copy(v.spr, s.pic, v.x, dy);
                        if v.x <= -v.w then
                          s.sprites[i] = nil;
                        else
                          local po = v.x + v.w;
                          if po > pos then
                            pos = po;
                          end
                        end
                      end
                    end
                    
                    if pos + 100 < 800 then
                      s._num = s._num + 1;
                      if s.titles[s._num] ~= nil then
                        local h,n,mw,spr = s:drawitem(s._num);
                        s.spritelen = s.spritelen+1;
                        table.insert(s.sprites, {h=h,n=n,spr=spr,w=mw,x=pos+100});
                      end
                    end
                  end
  ,drawitem     = function(s,idx)
                    local t = s.titles[idx];
                    local mw = 0;
                    
                    for i,v in pairs(t) do
                      local fn,fs,txt = s:font(v);
                      local rw = drawing.text_width(txt, fn, fs);
                      if rw > mw then
                        mw = rw;
                      end
                    end
                    
                    local spr = sprite.box(mw+10, 300, "black");
                    local y = 0;
                    
                    for i,v in pairs(t) do
                      local fn,fs,txt,fh = s:font(v);
                      drawing.draw_text_center(spr, txt, mw, y, "gray", fn, fs, true);
                      y = y + fh + 5;
                    end
                    
                    return y,n,mw,spr;
                  end
  ,sprites      = {}
  ,spritelen    = 0
  ,font         = function(s,obj)
                    if obj.type == "title" then
                      if s.cache_tt == nil then
                        s.cache_tt = drawing.text_height("sans-b.ttf", 16);
                      end
                      return "sans-b.ttf",16, obj.txt,s.cache_tt;
                    elseif obj.type == "regular" then
                      if s.cache_tr == nil then
                        s.cache_tr = drawing.text_height("sans.ttf", 20);
                      end
                      return "sans.ttf", 20, obj.txt,s.cache_tr;
                    elseif obj.type == "small" then
                      if s.cache_ts == nil then
                        s.cache_ts = drawing.text_height("sans.ttf", 14);
                      end
                      return "sans.ttf",14, obj.txt,s.cache_ts;
                    end
                  end
  ,titles       = {
                    {
                      tt_ "платформа INSTEAD и модуль МЕТАПАРСЕР",
                      tr_ "ПЁТР КОСЫХ"
                    },
                    {
                      tt_ "оригинальный сценарий и программирование",
                      tr_ "ВАСИЛИЙ ВОРОНКОВ"
                    },
                    {
                      tt_ "музыка",
                      tr_ "ВАСИЛИЙ ВОРОНКОВ"
                    },
                    {
                      tt_ "музыка создавалась в",
                      tr_ "ALCHEMY & GARAGE BAND & AUDACITY"
                    },
                    {
                      tt_ "обработка графики",
                      tr_ "PAINT.NET & GIMP"
                    },
                    {
                      tt_ "графика",
                      tr_ "flickr.com"
                    },
                    {
                      tt_ "звуковые эффекты",
                      tr_ "freesound.org"
                    },
                    {
                      tt_ "фотографии",
                      ts_ "flickr.com/photos/nasamarshall/4971286005",
                      ts_ "flickr.com/photos/nasamarshall/6099580967",
                      ts_ "flickr.com/photos/nasamarshall/7045389347",
                      ts_ "flickr.com/photos/nasamarshall/4998376359",
                      ts_ "flickr.com/photos/zanotti/312159382",
                      ts_ "flickr.com/photos/wonderferret/2826778639",
                      ts_ "flickr.com/photos/retrogui_photos/6830698011",
                      ts_ "flickr.com/photos/jpstanley/1423479417",
                      ts_ "flickr.com/photos/49968232@N00/375735252",
                      ts_ "flickr.com/photos/75929846@N00/6178788734",
                      ts_ "flickr.com/photos/44071822@N08/4091034625",
                      ts_ "flickr.com/photos/54299533@N04/6117599621"
                    },
                    {
                      tt_ "звуковые эффекты",
                      ts_ "freesound.org/people/themfish/sounds/34200",
                      ts_ "freesound.org/people/StephenSaldanha/sounds/166515",
                      ts_ "freesound.org/people/Robinhood76/sounds/83962",
                      ts_ "freesound.org/people/klangmanufaktur/sounds/144927",
                      ts_ "freesound.org/people/CGEffex/sounds/98332",
                      ts_ "freesound.org/people/lluiset7/sounds/141333",
                      ts_ "freesound.org/people/peepholecircus/sounds/169994",
                      ts_ "freesound.org/people/Mbazzy/sounds/6805",
                      ts_ "freesound.org/people/Freezeman/sounds/153213"
                    }
                  }
  ,stop         = 2150
  ,next         = walk_("films")
  ,rate         = 50
  ,dispose      = function(s)
                    s._x = nil;
                    s._num = 0;
                    for i,v in pairs(s.sprites) do
                      sprite.free(v.spr);
                    end
                    s.sprites={};
                    if cache_tt~=nil then
                      sprite.free_font(cache_tt);
                    end
                    if cache_tr~=nil then
                      sprite.free_font(cache_tr);
                    end
                    if cache_ts~=nil then
                      sprite.free_font(cache_ts);
                    end
                  end
}


---------------------------------------------------------------------------------------------------
--opentitle
---------------------------------------------------------------------------------------------------
films_opentitle = gfxroom {
   enter        = music_("orbit")
  ,ontimer      = function(s)
                    if s._frames == 495 then
                      mute_(0,0)();
                      sound_("bam")();
                    end
                    if s._x == nil then
                      s._x = 0;
                    end
                    if s._x > 1300 then
                      return;
                    end
                    local capt = drawing.font("sans-b.ttf", 92);
                    local gfx = s:loadgfx("orbit");
                    sprite.copy(gfx, s._x, 0, 800, 600, s.pic, 0, 0);
                    
                    s._x = s._x + 3;
                    
                    local y = 30;
                    local xs = 70;
                    local sx = 150;
                    local function al(x)
                      local r = s._x-x-sx;
                      if r < 255 then
                        return r;
                      end
                    end
                    if s._x > sx+(150*5) then
                      local t = sprite.text(capt, "Я", "white");
                      sprite.draw(t, s.pic, xs+600, y, al(750));
                      sprite.free(t);
                    end
                    if s._x > sx+(150*4) then
                      local t = sprite.text(capt, "И", "white");
                      sprite.draw(t, s.pic, xs+450, y, al(600));
                      sprite.free(t);
                    end
                    if s._x > sx+(150*3) then
                      local t = sprite.text(capt, "Д", "white");
                      sprite.draw(t, s.pic, xs+300, y, al(450));
                      sprite.free(t);
                    end
                    if s._x > sx+(150*2) then
                      local t = sprite.text(capt, "И", "white");
                      sprite.draw(t, s.pic, xs+150, y, al(300));
                      sprite.free(t);
                    end
                    if s._x > sx+150 then
                      local t = sprite.text(capt, "Л", "white");
                      sprite.draw(t, s.pic, xs, y, al(150));
                      sprite.free(t);
                    end
                  end
  ,stop         = 500
  ,next         = chain_ {
                    nil_("_x"),
                    walkfast2_("films")
                  }
  ,rate         = 60
}


---------------------------------------------------------------------------------------------------
--morning
---------------------------------------------------------------------------------------------------
films_morning = gfxroom {
   enter        = music_("morning")
  ,ontimer      = function(s)
                    if s._y == nil then
                      s._y = 900;
                    end
                    local gfx = s:loadgfx("gradient");
                    sprite.copy(gfx, 0, s._y, 800, 600, s.pic, 0, 0);
                    if s._y > 0 then
                      s._y = s._y - 3;
                    end
                  end
  ,stop         = 350
  ,next         = chain_ {
                    mute_(3000),
                    walk_("films")
                  }
  ,dispose      = nil_("_y")
  ,rate         = 60
}

---------------------------------------------------------------------------------------------------
--holo
---------------------------------------------------------------------------------------------------
films_holo = gfxroom {
   enter        = chain_ {
                    mute_(0,0),
                    light_(false),
                    music_("flash")
                  }
  ,ontimer      = function(s)
                    if s._y == nil then
                      s._y = -900;
                    end
                    local gfx = s:loadgfx("planet");
                    sprite.copy(gfx, s.pic, 0, s._y);
                    if s._frames > 50 and s._y < 0 then
                      s._y = s._y + 1;
                    end
                    if s._frames == 51 then
                      play_music("droplet");
                    end
                  end
  ,stop         = 1000
  ,next         = walk_("films")
  ,dispose      = nil_("_y")
  ,rate         = 50
}


---------------------------------------------------------------------------------------------------
--stars
---------------------------------------------------------------------------------------------------
films_stars = gfxroom {
   enter        = music_("morning")
  ,ontimer      = function(s)
                    if s._x == nil then
                      s._x = 0;
                    end
                    if s._x > 1200 then
                      return;
                    end
                    local gfx = s:loadgfx("stars");
                    sprite.copy(gfx, s._x, 0, 800, 600, s.pic, 0, 0);
                    s._x = s._x + 2;
                  end
  ,stop         = 650
  ,dispose      = nil_("_x")
  ,next         = chain_ {
                    mute_(3000),
                    walk_("films")
                  }
  ,rate         = 40
}


---------------------------------------------------------------------------------------------------
--KZ
---------------------------------------------------------------------------------------------------
films_kz = gfxroom {
   enter        = music_("macros")
  ,ontimer      = function(s)
                    if s._y == nil then
                      s._y = 70;
                    end
                    if s._x == nil then
                      s._x = 20;
                    end
                    local gfx = s:loadgfx("stripe");
                    sprite.copy(gfx, s._x, s._y, 800, 600, s.pic, 0, 0);
                    if s._x < 1000 then
                      s._x = s._x + 1;
                      if math.mod(s._x,2) == 0 then
                        s._y = s._y + 1;
                      end
                    else
                      if math.mod(s._frames,2) == 0 then
                        s._y = s._y + 1;
                      end
                    end
                  end
  ,stop         = 1200
  ,next         = walk_("films")
  ,dispose      = chain_ {
                    nil_("_y"),
                    nil_("_x")
                  }
  ,rate         = 50
}


---------------------------------------------------------------------------------------------------
--orbit
---------------------------------------------------------------------------------------------------
films_orbit = gfxroom {
   ontimer      = function(s)
                    if s._x == nil then
                      s._x = -527;
                    end
                    if s._x > -2 then
                      return;
                    end
                    local gfx = s:loadgfx("corona");
                    sprite.copy(gfx, s.pic, s._x, 0);
                    if s._frames >= 40 then
                      s._x = s._x + 2;
                    end
                    
                    if s._frames == 20 then
                      play_music("orbit_alt");
                    end
                  end
  ,stop         = 340
  ,next         = chain_ {
                    mute_(3000),
                    walk_("films")
                  }
  ,rate         = 70
  ,dispose      = nil_("_x")
}


---------------------------------------------------------------------------------------------------
--ahill
---------------------------------------------------------------------------------------------------
films_ahill = gfxroom {
   ontimer      = function(s)
                    if s._frames < 280 then
                      local x = 230;
                      if s._y == nil then
                        s._y = 600;
                      end
                      local gfx = s:loadgfx("ship");
                      sprite.copy(gfx, s.pic, x, s._y);
                      s._y = s._y - 1;
                      if s._frames == 1 then
                        play_music("orbit_alt");
                      end
                    end
                  end
  ,stop         = 300
  ,next         = walk_("films")
  ,rate         = 80
  ,dispose      = nil_("_y")
}
